﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MyHealthOnline.Dal.Generated.EntityClasses;
using MyHealthOnline.Dal.Generated.CollectionClasses;
using MyHealthOnline.Dal.Generated.HelperClasses;

namespace MyHealthOnline.Business
{
    public class ConsultBusiness
    {
        public static ConsultCollection GetSpecialistConsults(ConsultCollection consults, string specialistId, int patientId)
        {
            PredicateExpression filter = new PredicateExpression(ConsultFields.SpecialistId == specialistId);
            filter.AddWithAnd(ConsultFields.PatientId == patientId);
            filter.Add(new FieldCompareRangePredicate(ConsultFields.Id, consults.Items.Select(c => c.Id).ToArray()));
            ConsultCollection newConsultColl=new ConsultCollection();

            newConsultColl.GetMulti(filter);
            return newConsultColl;
        }

        public static ConsultEntity GetConsult(int consultId)
        {
            try
            {
                PredicateExpression filter = new PredicateExpression(ConsultFields.Id == consultId);
                ConsultCollection consultColl = new ConsultCollection();
                consultColl.GetMulti(filter);

                if (consultColl.Count() > 0)
                {
                    return consultColl.ElementAt(0);
                }
                else return null;
                //var consultList =
                //            (from cons in consultColl
                //             select new ConsultDto
                //             {
                //                 ConsultId = cons.Id,
                //                 PatientId = cons.Pacient.Id,
                //                 SpecialistId = cons.SpecialistId,
                //                 ConsultDate = Convert.ToDateTime(cons.ConsultDate),
                //                 PatientNo = cons.Pacient.Id,
                //                 Name = cons.Pacient.Name,
                //                 BirthDate = Convert.ToDateTime(cons.Pacient.Birthday),
                //                 LastDate = Convert.ToDateTime(cons.LatestContact),
                //                 Init = "",
                //                 Status = cons.Status,
                //                 Email = cons.Pacient.Email,
                //                 Mobile = cons.Pacient.Phone
                //             }
                //            );

                //return consultList.FirstOrDefault();
            }
            catch (ORMFieldIsNullException e)
            {
                return null;
            }

        }

        public static ConsultCollection GetOpenedConsults()
        {
            try
            {
                PredicateExpression filter = new PredicateExpression(ConsultFields.Status != "Closed");
                ConsultCollection consultColl = new ConsultCollection();
                consultColl.GetMulti(filter);

                return consultColl;
            }
            catch (ORMFieldIsNullException e)
            {
                return null;
            }
        }

        public static ConsultCollection GetClosedConsults()
        {
            try
            {
                PredicateExpression filter = new PredicateExpression(ConsultFields.Status == "Closed");
                ConsultCollection consultColl = new ConsultCollection();
                consultColl.GetMulti(filter);

                return consultColl;
                //var consultList =
                //            (from cons in consultColl
                //             select new ConsultDto
                //             {
                //                 ConsultId = cons.Id,
                //                 PatientId = cons.Pacient.Id,
                //                 PatientNo = cons.Pacient.Id,
                //                 SpecialistId = cons.SpecialistId,
                //                 Name = cons.Pacient.Name,
                //                 BirthDate = Convert.ToDateTime(cons.Pacient.Birthday),
                //                 LastDate = cons.LatestContact,
                //                 Init = "",
                //                 Status = cons.Status
                //             }
                //            );

                //return consultList;
            }
            catch (ORMFieldIsNullException e)
            {
                return null;
            }
        }

        public static ConsultCollection GetConsults()
        {
            try
            {
                ConsultCollection consultColl = new ConsultCollection();
                consultColl.GetMulti(null);
                return consultColl;
                //var consultList =
                //            (from cons in consultColl
                //                select new ConsultDto
                //                {
                //                    ConsultId = cons.Id,
                //                    ConsultDate = cons.ConsultDate,
                //                    PatientNo = cons.Pacient.Id,
                //                    PatientId = cons.Pacient.Id,
                //                    SpecialistId = cons.SpecialistId,
                //                    Name = cons.Pacient.Name,
                //                    BirthDate = Convert.ToDateTime(cons.Pacient.Birthday),
                //                    LastDate = cons.LatestContact,
                //                    Init = "",
                //                    Status = cons.Status,
                //                    ImageUrl = (cons.Status == "") ? "" : ""
                //                }
                //            );
                //return consultList;
            }
            catch (ORMFieldIsNullException e)
            {
                return null;
            }
        }

        public static ConsultCollection GetFutureConsults()
        {
            try
            {
                int weekNo = Utility.GetWeek(DateTime.Now);
                
                PredicateExpression filter = new PredicateExpression();
                filter.Add(new FieldCompareValuePredicate(ConsultFields.ConsultDate, ComparisonOperator.GreaterEqual,
                    Utility.GetStartOfWeek(weekNo).AddDays(7)));

                ConsultCollection consultList = GetConsults();

                filter.Add(new FieldCompareRangePredicate(ConsultFields.Id, consultList.Items.Select(c => c.Id).ToArray()));
           
                consultList.GetMulti(filter);
                
                return consultList;
            }
            catch (ORMFieldIsNullException e)
            {
                return null;
            }
        }

        public static ConsultCollection GetCurrentConsults(ConsultCollection allConsults)
        {
            int currWeekNo = Utility.GetWeek(DateTime.Now);
            var thisWeekConsultList = GetConsults(allConsults, Utility.GetStartOfWeek(currWeekNo));
            return thisWeekConsultList;
        }

        public static ConsultCollection GetConsults(ConsultCollection allConsults, DateTime startOfWeek)
        {
            PredicateExpression filter=new PredicateExpression();
            filter.Add(new FieldCompareValuePredicate(ConsultFields.ConsultDate, ComparisonOperator.GreaterEqual,
                startOfWeek));
            filter.AddWithAnd(new FieldCompareValuePredicate(ConsultFields.ConsultDate, ComparisonOperator.LesserThan,
                startOfWeek.AddDays(7)));

            filter.Add(new FieldCompareRangePredicate(ConsultFields.Id, allConsults.Items.Select(c => c.Id).ToArray()));
            ConsultCollection consults = new ConsultCollection();
            consults.GetMulti(filter);
            return consults;
        }

        public static bool SaveConsult(ConsultEntity consult, string conclusionText, string summary, string status)
        {
            try
            {
                consult.Conclusion = conclusionText;
                consult.Status = status;
                consult.Save();

                //ConclusionTemplateEntity conclusion = new ConclusionTemplateEntity();
                //conclusion.DoctorMsg = conclusionText;
                //conclusion.Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void CreateConsult(string specialistId, string personalNotes, string consultDate, int patientId)
        {
            ConsultEntity consult = new ConsultEntity();
            consult.Conclusion = "";
            consult.SpecialistId = specialistId;
            consult.PersonalNotes = personalNotes;
            consult.ConsultDate = Convert.ToDateTime( consultDate);
            consult.PatientId = patientId;
            consult.CreationDate = DateTime.Now;
            consult.LatestContact = DateTime.Now;
            consult.Status = MessageStatus.GetMessageStatus( MessageStatusEnum.WaitingForSpecialistResponse);
            consult.Save();

            AppProposalBusiness.AddAppointment(
                consult.Id,
                "",
                consult.ConsultDate.ToString(),
                "");
        }

        //public static bool Save(ConsultDto consultDto)
        //{
        //    try
        //    {
        //        ConsultEntity consult;

        //        if (consultDto.ConsultId > 0)
        //        {
        //            consult = new ConsultEntity();
        //        }
        //        else
        //        {
        //            PredicateExpression filter = new PredicateExpression(ConsultFields.Id == consultDto.ConsultId);
        //            ConsultCollection consultColl = new ConsultCollection();
        //            consultColl.GetMulti(filter);

        //            consult = consultColl.FirstOrDefault();
        //        }

        //        consult.CreationDate = consultDto.CreationDate;
        //        consult.SpecialistId = consultDto.SpecialistId;
        //        consult.PersonalNotes = consult.PersonalNotes;
        //        consult.ConsultDate = consultDto.ConsultDate;
        //        consult.PatientId = consultDto.PatientId;
        //        consult.ConsultDate = consult.ConsultDate;
        //        consult.LatestContact = consult.LatestContact;
        //        consult.Status = consultDto.Status;

        //        consult.Save();

        //        return true;
        //    }
        //    catch (ORMFieldIsNullException e)
        //    {
        //        return false;
        //    }
        //}

        public static ConsultCollection FilterConsults(ConsultCollection consults, string patientName)
        {
            PredicateExpression filter = new PredicateExpression();
            if (patientName.Length > 0)
            {
                filter.Add(new FieldLikePredicate(PatientFields.Name, "%"+patientName+"%"));
            
                int nr = 0;
                if (int.TryParse(patientName, out nr))
                {
                    filter.AddWithOr(PatientFields.Id == nr);
                }
            }
            PredicateExpression consultFilter = new PredicateExpression();
            consultFilter.Add(new FieldCompareRangePredicate(ConsultFields.Id,consults.Items.Select(c=>c.Id).ToArray()));
            RelationCollection relColl=new RelationCollection();
            relColl.Add(ConsultEntity.Relations.PatientEntityUsingPatientId).CustomFilter = filter;
            ConsultCollection consultList = new ConsultCollection();
            consultList.GetMulti(consultFilter, 0, null, relColl);
            return consultList;
             
        }

        public static void UpdateConsultStatus(ConsultEntity consult, MessageStatusEnum status)
        {
            consult.Status = MessageStatus.GetMessageStatus(status);
            consult.Save();
        }
    }
}
